package com.example.gzysmartcity.ui.fragment.idea;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.SearchView;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.blankj.utilcode.util.ToastUtils;
import com.example.gzysmartcity.R;
import com.example.gzysmartcity.databinding.FragmentPositionBinding;
import com.example.gzysmartcity.repository.model.PositionUesr;
import com.example.gzysmartcity.ui.adapter.PositionRvAdapter;
import com.example.gzysmartcity.ui.base.BaseFragment;
import com.example.gzysmartcity.viewmodel.PositionViewModel;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

public class PositionFragment extends BaseFragment<FragmentPositionBinding> {
//    private boolean search;

    @Override
    protected FragmentPositionBinding viewBinding() {
        return FragmentPositionBinding.inflate(getLayoutInflater(), baseBinding.getRoot(), true);
    }

    @Override
    public void onViewCreated(@NonNull @NotNull View view, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        PositionViewModel positionViewModel = new ViewModelProvider(this).get(PositionViewModel.class);
        positionViewModel.getPositionLinesData();
        b.rfIdea.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull @NotNull RefreshLayout refreshLayout) {
                ToastUtils.showShort("没有更多信息了");
                b.rfIdea.closeHeaderOrFooter();

            }

            @Override
            public void onRefresh(@NonNull @NotNull RefreshLayout refreshLayout) {
                positionViewModel.getPositionLinesData();
            }
        });
        positionViewModel.getPositionsLiveData().observe(getViewLifecycleOwner(), state -> {
            switch (state) {
                case SUCCESS:
                    b.rfIdea.closeHeaderOrFooter();
                    List<PositionUesr> positionUesrs = new ArrayList<>(positionViewModel.getPositionUesrList());
                    PositionRvAdapter adapter = new PositionRvAdapter(R.layout.rv_position);
                    adapter.setNewInstance(positionUesrs);
                    b.rvPosition.setLayoutManager(new LinearLayoutManager(getContext()));
                    b.rvPosition.setAdapter(adapter);
                    b.searchPosition.setSubmitButtonEnabled(true);
                    b.searchPosition.setOnCloseListener(() -> {
                        positionUesrs.clear();
                        positionUesrs.addAll(positionViewModel.getPositionsSreach(""));
                        adapter.notifyDataSetChanged();
                        return false;
                    });
                    b.searchPosition.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
                        @Override
                        public boolean onQueryTextSubmit(String query) {
                            b.searchPosition.setIconified(true);
                            positionUesrs.clear();
                            positionUesrs.addAll(positionViewModel.getPositionsSreach(query));
                            adapter.notifyDataSetChanged();
                            return true;
                        }

                        @Override
                        public boolean onQueryTextChange(String newText) {
                            return false;
                        }
                    });
                    adapter.setOnItemClickListener((adapter1, view1, position) -> {
                        Bundle bundle = new Bundle();
                        int typeId = positionUesrs.get(position).getType();
                        bundle.putInt("PositiontypeId", typeId);
                        jump(PersonnelFragment.class, bundle, null);
                    });
                    break;
                case FAIL:
                    ToastUtils.showShort("请求失败,请尝试下拉重新请求数据。");
                    b.rfIdea.closeHeaderOrFooter();
                    break;
            }
        });

    }

    @Override
    protected String setTitle() {
        return "岗位市场";
    }

    @Override
    protected boolean showBack() {
        return true;
    }

    @Override
    protected Boolean showBottom() {
        return null;
    }
}
